package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec701Entity;
import net.yunxinyong.cec.entity.Cec701EntityExample;
import net.yunxinyong.cec.mapper.Cec701EntityMapper;
import net.yunxinyong.cec.service.Cec701Service;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class Cec701ServiceImpl implements Cec701Service {
    @Autowired
    private Cec701EntityMapper cec701EntityMapper;

    /*
     * 查询所有企业信息
     * */
    @Override
    public PageBean findAll(int page, int rows, Cec701Entity cec701) {
        Cec701EntityExample cec701EntityExample = new Cec701EntityExample();
        Cec701EntityExample.Criteria criteria = cec701EntityExample.createCriteria();
        //判断查询条件,如果实体类中有值，就是条件查询
        if (cec701.getId() != null) {
            criteria.andIdEqualTo(cec701.getId());
        }
        if (page != 0 && rows != 0) {
            PageHelper.startPage(page, rows);
        }
        List<Cec701Entity> list = cec701EntityMapper.selectByExample(cec701EntityExample);
        //设置分页信息
        PageBean<Cec701Entity> pageBean = new PageBean<>();
        PageInfo<Cec701Entity> pageInfo = new PageInfo<>(list);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageDatas(list);
        return pageBean;
    }

    /**
     * 根据id修改信息
     */
    @Override
    public int updateById(int id, int status) {
        Cec701Entity cec701Entity = new Cec701Entity();
        cec701Entity.setId(id);
        cec701Entity.setStatus(status);
        cec701Entity.setUpdateTime(new Date());
        int count = cec701EntityMapper.updateByPrimaryKeySelective(cec701Entity);
        return count;
    }

    /**
     * 根据id删除信息
     */
    @Override
    public int deleteById(int id) {
        Cec701Entity cec701Entity = new Cec701Entity();
        cec701Entity.setId(id);
        cec701Entity.setStatus(1);
        int count = cec701EntityMapper.updateByPrimaryKeySelective(cec701Entity);
        return count;
    }

    /**
     * 添加信息
     */
    @Override
    public int insertOne() {
        Cec701Entity cec701Entity = new Cec701Entity();
        //更改表时间
        cec701Entity.setUpdateTime(new Date());
        //创建表时间
        cec701Entity.setCreateTime(new Date());
        int count = cec701EntityMapper.insertSelective(cec701Entity);
        return count;
    }

    /**
     * 根据baseid查询信息
     */
    @Override
    public List<Cec701Entity> findOneById(int id) {
        Cec701EntityExample cec701EntityExample = new Cec701EntityExample();
        Cec701EntityExample.Criteria criteria = cec701EntityExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Cec701Entity> list = cec701EntityMapper.selectByExample(cec701EntityExample);
        return list;
    }

}
